#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDebug>
#include <QFile>
#include <QFileDialog>
#include <QTime>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_clicked()
{
    QString srcChar = "/";
    QString dstChar = "\\";
    if(ui->comboBox_trans->currentIndex() == 1) {
        srcChar = "\\";
        dstChar = "/";
    }
    QStringList strList = ui->lineEdit->text().split(srcChar);

    ui->lineEdit->setText(strList.join(dstChar));
}

void MainWindow::on_pushButton_extart_clicked()
{
    QString fileName;
    QFile file;
    QString lineContent;
    QTextStream in;
    int s32LineNum;

    deinitExtart();
    initExtart();

    fileName = QFileDialog::getOpenFileName(this);
    qDebug() << fileName;

    file.setFileName(fileName);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return;

    in.setDevice(&file);
    in.setCodec("UTF-8");

    while (!in.atEnd()) {
        lineContent = in.readLine();
        processLine(s32LineNum++, lineContent);
    }

    ui->tabWidget->setCurrentIndex(1);

}

void MainWindow::processLine(int s32LineNum, QString lineContent)
{
    /* 如果把关键内容，再高亮就好多了，可以选择高亮的方式。 */

    for (int i = 0; i < m_keyStrList.count(); ++i) {
        if(lineContent.contains(m_keyStrList.at(i))){
            QString string = QString("<b>%1</b>").arg(s32LineNum);
            m_textBrowserVector.at(i)->append(string + lineContent);
        }
    }
}


void MainWindow::initExtart()
{
    QString text = ui->lineEdit_extart1->text();
    if(text.length() > 0) {
        m_keyStrList.append(text);
    }
    text = ui->lineEdit_extart2->text();
    if(text.length() > 0) {
        m_keyStrList.append(text);
    }
    text = ui->lineEdit_extart3->text();
    if(text.length() > 0) {
        m_keyStrList.append(text);
    }

    m_textBrowserVector.append(ui->textBrowser);
    m_textBrowserVector.append(ui->textBrowser_2);
    m_textBrowserVector.append(ui->textBrowser_3);

}

void MainWindow::deinitExtart()
{
    m_keyStrList.clear();
    ui->tabWidget->setCurrentIndex(0);
}

///STRING转HEX
void MainWindow::on_pushButton_2_clicked()
{
    QString string = ui->lineEdit_hex_str->text();

    QByteArray hexArray = string.toLatin1();
    QString tempStr = QString::fromLatin1(hexArray.toHex().toUpper());

    ui->lineEdit_hex_str->setText(tempStr);
}

///HEX转STRING
void MainWindow::on_pushButton_3_clicked()
{
   QString string = ui->lineEdit_hex_str->text();
   QByteArray hexArray = QByteArray::fromHex(string.toLatin1());

   ui->lineEdit_hex_str->setText(QString::fromLatin1(hexArray));
}

void MainWindow::on_pushButton_4_clicked()
{
    QStringList strList =  ui->lineEdit_dateTime->text().split("~");
    qDebug() << strList;
    QTime time1 = QTime::fromString(strList.first(), "hh:mm");
    QTime time2 = QTime::fromString(strList.last(), "hh:mm");

    qDebug() << time1 << time2 <<  time1.secsTo(time2);
}
